cmake_minimum_required(VERSION 3.10.0)
project(SignatureTool VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


# 查找Qt6需要的组件
find_package(Qt6 REQUIRED COMPONENTS
    Core
    Gui
    Widgets
)

# 启用自动处理moc、rcc和uic
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# # 启用多线程支持(用于性能测试)
# set(THREADS_PREFER_PTHREAD_FLAG ON)
# find_package(Threads REQUIRED)



set(Cryptography_Dir ${CMAKE_CURRENT_SOURCE_DIR})
set(Cryptography_Src_Dir ${Cryptography_Dir}/source)
set(Cryptography_Lib_Dir ${Cryptography_Dir}/library)
set(Cryptography_Include_Dir ${Cryptography_Dir}/include)

include_directories(${Cryptography_Include_Dir})

# SignatureTool.exe
add_executable(SignatureTool ${Cryptography_Src_Dir}/main.cpp)
target_sources(SignatureTool PUBLIC ${Cryptography_Src_Dir}/GUI.cpp)

target_sources(SignatureTool PUBLIC ${Cryptography_Src_Dir}/signature_manager/data_conversion.cpp)
target_sources(SignatureTool PUBLIC ${Cryptography_Src_Dir}/signature_manager/signature_manager.cpp)

target_sources(SignatureTool PUBLIC ${Cryptography_Src_Dir}/signature_manager/Hash.cpp)
target_sources(SignatureTool PUBLIC ${Cryptography_Src_Dir}/signature_manager/GMPInt.cpp)

target_sources(SignatureTool PUBLIC ${Cryptography_Src_Dir}/signature_manager/RSA.cpp)
target_sources(SignatureTool PUBLIC ${Cryptography_Src_Dir}/signature_manager/DSA.cpp)
target_sources(SignatureTool PUBLIC ${Cryptography_Src_Dir}/signature_manager/Rabin.cpp)
target_sources(SignatureTool PUBLIC ${Cryptography_Src_Dir}/signature_manager/ECDSA.cpp)
target_sources(SignatureTool PUBLIC ${Cryptography_Src_Dir}/signature_manager/EdDSA.cpp)

target_link_libraries(SignatureTool PRIVATE ${Cryptography_Lib_Dir}/gmp.lib)

# SignatureTracyTest.exe
add_executable(SignatureTracyTest ${Cryptography_Src_Dir}/main_tracy_test.cpp)

target_sources(SignatureTracyTest PUBLIC ${Cryptography_Src_Dir}/signature_manager/data_conversion.cpp)
target_sources(SignatureTracyTest PUBLIC ${Cryptography_Src_Dir}/signature_manager/signature_manager.cpp)

target_sources(SignatureTracyTest PUBLIC ${Cryptography_Src_Dir}/signature_manager/Hash.cpp)
target_sources(SignatureTracyTest PUBLIC ${Cryptography_Src_Dir}/signature_manager/GMPInt.cpp)

target_sources(SignatureTracyTest PUBLIC ${Cryptography_Src_Dir}/signature_manager/RSA.cpp)
target_sources(SignatureTracyTest PUBLIC ${Cryptography_Src_Dir}/signature_manager/DSA.cpp)
target_sources(SignatureTracyTest PUBLIC ${Cryptography_Src_Dir}/signature_manager/Rabin.cpp)
target_sources(SignatureTracyTest PUBLIC ${Cryptography_Src_Dir}/signature_manager/ECDSA.cpp)
target_sources(SignatureTracyTest PUBLIC ${Cryptography_Src_Dir}/signature_manager/EdDSA.cpp)

target_link_libraries(SignatureTracyTest PRIVATE ${Cryptography_Lib_Dir}/gmp.lib)
target_include_directories(SignatureTracyTest PUBLIC ./dependencies/tracy/public/tracy)


add_library(TracyClient STATIC ./dependencies/tracy/public/TracyClient.cpp)
target_include_directories(TracyClient PUBLIC ./dependencies/tracy/public/tracy)
target_compile_definitions(TracyClient PUBLIC TRACY_ENABLE=1)
target_compile_definitions(TracyClient PUBLIC TRACY_VERBOSE=1)

target_include_directories(SignatureTool PUBLIC ./dependencies/tracy/public/tracy)
target_link_libraries(SignatureTracyTest PUBLIC TracyClient ${CMAKE_DL_LIBS} Threads::Threads)


# debug模式 添加额外的日志输出信息
if(CMAKE_BUILD_TYPE EQUAL "Debug")
    add_definitions(-DDEBUG_PRINTS_LEVEL_3)
    message("Debug mode")
endif()


# 链接Qt6库
target_link_libraries(SignatureTool PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Widgets
)

# 获取 Qt6 安装根目录
get_filename_component(QT6_ROOT_DIR "${Qt6_DIR}" DIRECTORY)
get_filename_component(QT6_ROOT_DIR "${QT6_ROOT_DIR}" DIRECTORY)
get_filename_component(QT6_ROOT_DIR "${QT6_ROOT_DIR}" DIRECTORY)

# 设置 bin 路径
set(QT6_BIN_DIR "${QT6_ROOT_DIR}/bin")
message("${QT6_BIN_DIR}")

# 设置 plugins 路径
set(QT6_PLUGINS_DIR "${QT6_ROOT_DIR}/plugins")


# 定义要拷贝的DLL文件列表 
set(QT_REQUIRED_DLLS
    Qt6Cored.dll
    Qt6Guid.dll
    Qt6Widgetsd.dll
)

# 构建完成后执行拷贝操作
foreach(DLL_NAME ${QT_REQUIRED_DLLS})
    add_custom_command(TARGET SignatureTool POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${QT6_BIN_DIR}/${DLL_NAME}"
            $<TARGET_FILE_DIR:SignatureTool>
        COMMENT "正在拷贝 DLL 文件: ${DLL_NAME}"
    )
endforeach()

add_custom_command(TARGET SignatureTool POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${QT6_PLUGINS_DIR}/platforms
    $<TARGET_FILE_DIR:SignatureTool>/platforms
COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${QT6_PLUGINS_DIR}/styles
    $<TARGET_FILE_DIR:SignatureTool>/styles
COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${QT6_PLUGINS_DIR}/imageformats
    $<TARGET_FILE_DIR:SignatureTool>/imageformats
)


